package com.hotel.BL;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.Agency;
import data.CreditCard;
import data.Guest;
import data.ValidityConsole;

public class CreditCardLogic extends AbstractBL {
	public CreditCardLogic(HttpServletRequest req,HttpServletResponse resp){
		this.req = req;
		this.resp = resp;
	}
	public CreditCard GetCreditCard(long id) {
		Key key = KeyFactory.createKey(EntityName.CreditCard, id);
		try {
			Entity lang = datastore.get(key);
			CreditCard c = new CreditCard();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}

	public CreditCard GetCreditCard(long hotelId, String title) {
		Query q = new Query(EntityName.CreditCard);

		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterStatus = new FilterPredicate("status",
				FilterOperator.EQUAL, true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus);

		q.setFilter(filter);

		return GetCreditCard(datastore.prepare(q).asSingleEntity());
	}

	public List<CreditCard> GetAllCreditCard(long hotelId) {
		Query q = new Query(EntityName.CreditCard);

		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterStatus = new FilterPredicate("status",
				FilterOperator.EQUAL, "true");

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus);

		q.setFilter(filter);

		return GetList(datastore.prepare(q).asList(
				FetchOptions.Builder.withDefaults()));
	}

	public CreditCard GetCreditCard(Entity entity) {
		CreditCard item = new CreditCard();
		item.setEntity(entity);
		return item;
	}

	public List<CreditCard> GetList(List<Entity> entities) {
		List<CreditCard> list = new ArrayList<CreditCard>();
		if(entities!=null)
		for (Entity e : entities) {
			CreditCard a = new CreditCard();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}

	public CreditCard AddCreditCard(long hotelId, String CVC, String CardType,
			String NameOnCard, String CardNumber, String ExpiryDate)
			throws EntityNotFoundException {
		CreditCard t = new CreditCard();
		t.setHotelId(hotelId);
		t.setCVC(CVC);
		t.setCardType(CardType);
		t.setNameOnCard(NameOnCard);
		t.setCardNumber(CardNumber);
		t.setExpiryDate(ExpiryDate);
		t.setStatus(true);

		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}

	public CreditCard EditCreditCard(long creditCardId,String CVC, String CardType,
			String NameOnCard, String CardNumber, String ExpiryDate)
			throws EntityNotFoundException {
		CreditCard t = GetCreditCard(creditCardId);
		t.setCVC(CVC);
		t.setCardType(CardType);
		t.setNameOnCard(NameOnCard);
		t.setCardNumber(CardNumber);
		t.setExpiryDate(ExpiryDate);

		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);		

		return t;
	}

	public boolean DeleteCreditCard(long creditCardId)
			throws EntityNotFoundException {
		CreditCard t = GetCreditCard(creditCardId);
		t.setStatus(false);

		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		if(t.isStatus()){
			return true;
		}else{
			return false;
		}
		
	}

	public CreditCard ChangeStatus(long creditCardId)
			throws EntityNotFoundException {

		CreditCard a = GetCreditCard(creditCardId);
		if (a.isStatus() == true) {
			a.setStatus(false);
		} else if (a.isStatus() == false) {
			a.setStatus(true);
		}

		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public CreditCard AddFullCreditCard(long hotelId, String CVC, String CardType,
			String NameOnCard, String CardNumber, String ExpiryDate,String sharer,
			String address,String state,String country,String city,String zipcode,String guestId)
			throws EntityNotFoundException {
		CreditCard t = new CreditCard();
		t.setHotelId(hotelId);
		t.setCVC(CVC);
		t.setCardType(CardType);
		t.setNameOnCard(NameOnCard);
		t.setCardNumber(CardNumber);
		t.setExpiryDate(ExpiryDate);
		t.setStatus(true);
		t.setSharer(sharer);
		t.setAddress(address);
		t.setState(state);
		t.setCountry(country);
		t.setCity(city);
		t.setZipcode(zipcode);
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);
		
		if(guestId != null){
			Guest g = new GuestLogic(req,resp).GetGuest(Long.parseLong(guestId));
			if(g.getCreditCardString()!=null){
				g.setCreditCardString(g.getCreditCardString()+","+t.getId());
			}else{
				g.setCreditCardString(String.valueOf(t.getId()));
			}
			Key kg = datastore.put(g.getEntity());
			g.setId(kg.getId());
			g.setKey(kg);
		}
		return t;
		
	}
	
	public CreditCard AddConsoleCreditCard(long hotelId, String CVC, String CardType,String NameOnCard, String CardNumber, String ExpiryDate,
			String address,String state,String country,String city,String zipcode,long consoleId)
			throws EntityNotFoundException {
		CreditCard t = new CreditCard();
		t.setHotelId(hotelId);
		t.setCVC(CVC);
		t.setCardType(CardType);
		t.setNameOnCard(NameOnCard);
		t.setCardNumber(CardNumber);
		t.setExpiryDate(ExpiryDate);
		t.setStatus(true);
		t.setAddress(address);
		t.setState(state);
		t.setCountry(country);
		t.setCity(city);
		t.setZipcode(zipcode);
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);
		
		if(consoleId != 0){
			Agency a = new AgencyLogic(req, resp).GetAgency(consoleId);
			if(a.getListcreditString()!=null&&!a.getListcreditString().equals("")){
				a.setListcreditString(a.getListcreditString()+","+t.getId());
			}else{
				a.setListcreditString(String.valueOf(t.getId()));
			}
			Key kg = datastore.put(a.getEntity());
			a.setId(kg.getId());
			a.setKey(kg);
		}
		return t;
	}
	
	public ValidityConsole AddValidityConsole(long hotelId, Date StartDate, Date EndDate,long id)
			throws EntityNotFoundException {
		ValidityConsole v = new ValidityConsole();
		v.setHotelId(hotelId);
		v.setStartdate(StartDate);
		v.setEnddate(EndDate);
		v.setConsoleId(id);
		Key k = datastore.put(v.getEntity());
		v.setId(k.getId());
		v.setKey(k);
		return v;
	}
	
	public String enCodeCreditCardNumber(String CCN){
		String enCCN = CCN;
		if(enCCN != null){
			String creditCardX ="XXXXXXXXXXXXXXXXXXXX";
			String text="";
			
			if(enCCN.length()==16){
				text+=enCCN.substring(0,4);
				text+=creditCardX.substring(0,enCCN.length()-8);
				text+=enCCN.substring(enCCN.length()-4,enCCN.length());
			}else{
				text=enCCN;
			}
			return text;
		}
		return "";
	}
	
	public boolean DeleteCreditCardConsole(long creditCardId,long consoleId)
			throws EntityNotFoundException {
		CreditCard t = GetCreditCard(creditCardId);
		t.setStatus(false);
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);
			Agency a = new AgencyLogic(req, resp).GetAgency(consoleId);
			if(a.getListcreditString()!=null){
				String[] listCredit = a.getListcreditString().split(",");
				String creditId = "";
				for(int i=0;i<listCredit.length;i++){
					if(Long.parseLong(listCredit[i])!=creditCardId){
						creditId += listCredit[i]+",";
					}
				}
				if(!creditId.equals(""))
					creditId = creditId.substring(0, creditId.length()-1);
				a.setListcreditString(creditId);
			}
			Key kg = datastore.put(a.getEntity());
			a.setId(kg.getId());
			a.setKey(kg);
		if(t.isStatus()){
			return true;
		}else{
			return false;
		}
		
	}
}
